﻿using Awsc.DataAcquisitionSys.Core.Common;
using Awsc.DataAcquisitionSys.Core.Databases;
using Awsc.DataAcquisitionSys.Core.Model;
using Awsc.DataAcquisitionSys.Core.Models.POCO;
using Awsc.DataAcquisitionSys.Core.Service.Alarm.Interfaces;


namespace Awsc.DataAcquisitionSys.Core.Service.Alarm.Implement
{
    public interface IAlarmRuleService
    {
        Task<IEnumerable<IAlarmRule>> GetAlarmRulesAsync();
        Task<IAlarmRule> GetByNameAsync(string ruleName);
        Task<IAlarmRule> GetByIdAsync(int id);
        Task AddRuleAsync(IAlarmRule rule);
        Task UpdateRuleAsync(IAlarmRule rule);
        Task RemoveRuleAsync(int id);
    }
    public class AlarmRuleService : IAlarmRuleService
    {
        private readonly IRepository<AlarmRuleModel> alarmRuleRepository;

        public AlarmRuleService(IRepository<AlarmRuleModel> alarmRuleRepository)
        {
            this.alarmRuleRepository = alarmRuleRepository;
        }

        public async Task<IEnumerable<IAlarmRule>> GetAlarmRulesAsync()
        {
            var allRulesModel = await alarmRuleRepository.GetAllAsync();
            var allRules = allRulesModel.Select(BuildAlarmRule);
            return allRules;
        }
        public async Task<IAlarmRule> GetByIdAsync(int id)
        {
            ArgumentAssert.GreaterThanZero(id);
            var ruleModel = await alarmRuleRepository.GetByIdAsync(id);
            return BuildAlarmRule(ruleModel);
        }
        public async Task<IAlarmRule> GetByNameAsync(string ruleName)
        {
            ArgumentAssert.NotNullOrEmpty(ruleName);
            var ruleModel = await alarmRuleRepository.GetByNameAsync(ruleName);
            return BuildAlarmRule(ruleModel);
        }
        public async Task AddRuleAsync(IAlarmRule rule)
        {
            ArgumentAssert.NotNull(rule);
            var ruleMode = BuildAlarmRuleMode(rule);
            await alarmRuleRepository.AddAsync(ruleMode);

        }
        public async Task UpdateRuleAsync(IAlarmRule rule)
        {
            ArgumentAssert.NotNull(rule);
            var ruleMode = BuildAlarmRuleMode(rule);
        }

        public async Task RemoveRuleAsync(int id)
        {
            ArgumentAssert.GreaterThanZero(id);
            await alarmRuleRepository.DeleteAsync(id);
        }


        private IAlarmRule BuildAlarmRule(AlarmRuleModel model)
        {
            IAlarmRule result;
            if (model.RuleType == AlarmRuleType.ConditionRule)
            {
                result = new ConditionAlarmRule();
            }
            else
            {
                result = new AlgorithmAlarmRule();
            }
            result.Id = model.Id;
            result.Condition = model.Condition;
            result.Name = model.Name;
            result.AlarmLevel = model.Level;
            result.Status = model.Status;
            result.IsBuildInRule = model.IsBuildIn;
            result.Description = model.Description;
            return result;
        }

        private AlarmRuleModel BuildAlarmRuleMode(IAlarmRule rule)
        {
            if (rule == null)
            {
                return null;
            }
            AlarmRuleModel model = new AlarmRuleModel
            {
                Id = rule.Id,
                Name = rule.Name,
                Level = rule.AlarmLevel,
                Status = rule.Status,
                IsBuildIn = rule.IsBuildInRule,
                Description = rule.Description,
                RuleType = rule.RuleType
            };
            return model;
        }
    }
}
